const express = require("express")
const router = express.Router()
const { getAll, getOne, exec } = require("../db")

/* 查询所有 */
router.get("/user", async function (req, res) {
  //1、解析请求的参数
  //2、编写sql语句
  let sql = `select * from gg_user`
  //3、执行sql语句返回数据
  try {
    const data = await getAll(sql)
    res.send({
      code: 0,
      msg: "获取所有用户成功",
      result: data,
    })
  } catch (error) {
    res.send({
      code: 000001,
      msg: "获取所有用户失败",
      result: debug ? error : "",
    })
  }
})

/* 查看用户是否重复 */
router.post("/useryornone", async (req, res) => {
  //1、解析请求参数
  const { user_name, user_password } = req.body
  // console.log(user_name, user_password)
  //2、编写sql语句
  const sql1 = `select  user_id,user_name,user_password,user_type from gg_user WHERE user_name='${user_name}' && user_password='${user_password}' && user_isDelete = 1`
  const sql2 = `update gg_user set user_onlinestatus='1' where user_name='${user_name}' && user_password='${user_password}'`
  //3、执行sql语句，返回数据
  try {
    const data = await getAll(sql1)
    if (data == "") {
      res.send({ code: 0, msg: "该用户没注册", result: data })
    } else if (data) {
      await exec(sql2)
      res.send({ code: 0, msg: "登录成功", result: data })
    }
  } catch (error) {
    res.send({
      code: 0,
      msg: "失败",
      result: debug ? error : "",
    })
  }
})

/* 根据id查询单个用户 */
router.get("/user/:id", async (req, res) => {
  //1、解析请求的参数
  const { id } = req.params
  //2、编写sql语句
  let sql = `select * from gg_user where user_id=${id}`
  //3、执行sql语句返回数据
  try {
    const data = await getOne(sql)
    res.send({
      code: 0,
      msg: "获取单个用户成功",
      result: data,
    })
  } catch (error) {
    res.send({
      code: 000002,
      msg: "获取单个用户失败",
      result: debug ? error : "",
    })
  }
})

/* 注册用户 */
router.post("/user", async (req, res) => {
  //1、解析请求的参数
  const content = req.body
  const {
    user_name,
    user_password,
    user_type,
    user_onlinestatus,
    user_isDelete,
  } = content
  //2、编写sql语句
  let sql1 = `SELECT user_name FROM gg_user WHERE user_name='${user_name}'`
  const data = await getAll(sql1)
  // const one =
  if (data[0]) {
    res.send({
      code: 000003,
      msg: "当前用户已经存在",
      result: data[0],
    })
  } else {
    let sql = `INSERT INTO gg_user(user_name,user_password,user_type,user_onlinestatus,user_isDelete) VALUES ('${user_name}','${user_password}','${user_type}','${user_onlinestatus}','${user_isDelete}')`
    // console.log(sql)
    try {
      const data = await exec(sql)
      res.send({
        code: 0,
        msg: "用户注册成功",
        result: data,
      })
    } catch (error) {
      res.send({
        code: 000003,
        msg: "用户注册失败",
        result: debug ? error : "",
      })
    }
  }
})

/* 修改用户 */
router.put("/user", async function (req, res) {
  //1、解析请求参数
  const content = req.body
  const key = Object.keys(content)
  const value = Object.values(content)
  try {
    if (key[1] == "user_password") {
      // console.log("修改密码", value[0], value[1])
      // 2、编写sql语句
      let sql = `UPDATE gg_user SET user_password='${value[1]}' WHERE user_name='${value[0]}'`
      console.log(sql)
      // 3、执行sql语句返回数据
      const data = await exec(sql)
      res.send({
        cose: 0,
        msg: "修改密码成功",
        result: data,
      })
    } else if (key[1] == "user_onlinestatus") {
      // console.log("修改状态", value[0], value[1])
      // 2、编写sql语句
      let sql = `UPDATE gg_user SET user_onlinestatus='${value[1]}' WHERE user_name='${value[0]}'`
      console.log(sql)
      //3.执行SQL语句返回数据
      const data = await exec(sql)
      res.send({
        code: 0,
        msg: "修改用户状态成功",
        result: data,
      })
    } else if (key[1] == "user_isDelete") {
      // console.log("删除用户", value[0], value[1])
      // 2、编写sql语句
      let sql = `UPDATE gg_user SET user_isDelete='${value[1]}' WHERE user_name='${value[0]}'`
      console.log(sql)
      const data = await exec(sql)
      res.send({
        code: 0,
        msg: "删除用户成功",
        result: data,
      })
    }
  } catch (error) {
    res.send({
      code: 000004,
      msg: "修改出错",
      result: debug ? error : "",
    })
  }
})

/* 按用户类型查询 */
router.post("/usersort", async (req, res) => {
  //1、解析请求参数
  const { user_type } = req.body
  //2、编写sql语句
  const sql = `select * from gg_user where user_type='${user_type}' and user_isDelete='1'`
  // 3、执行sql语句，返回数据
  try {
    const data = await getAll(sql)
    res.send({
      code: 0,
      msg: `'查询${user_type}权限用户'`,
      result: data,
    })
  } catch (error) {
    res.send({
      code: 0,
      msg: `'查询${user_type}权限用户失败'`,
      result: debug ? error : "",
    })
  }
})

/* 用户升降级 */
router.post("/userupdown", async function (req, res) {
  //1、解析请求参素
  const { user_name, user_type } = req.body
  console.log(user_name, user_type)
  //2、编写sql语句
  const sql = `UPDATE gg_user set user_type='${user_type}' where user_name='${user_name}'`
  //3、执行sql语句并返回数据
  try {
    const data = getAll(sql)
    res.send({
      code: 0,
      msg: "修改权限成功",
      result: data,
    })
  } catch (error) {
    res.send({
      code: 0,
      msg: "修改权限失败",
      result: debug ? error : "",
    })
  }
})

/* 根据用户名查询 */
router.put("/userone", async (req, res) => {
  const { user_name, user_password } = req.body
  const sql = `update gg_user set user_password=${user_password} where user_name='${user_name}'`
  try {
    const data = exec(sql)
    res.send({
      code: 0,
      msg: "成功",
      result: data,
    })
  } catch (error) {
    res.send({
      code: 00004,
      msg: "发生错误",
      result: debug ? error : "",
    })
  }
})

module.exports = router
